import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Session,
  Req,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiConsumes,
} from '@nestjs/swagger';
import { Request } from 'express';
import { JwtAuthGuard } from '@wwjCommon/security/guards/jwt-auth.guard';
import { RolesGuard } from '@wwjCommon/security/guards/roles.guard';
import { Roles } from '@wwjCommon/security/decorators/roles.decorator';
import { Public } from '@wwjCommon/security/decorators/public.decorator';
import { BusinessException } from '@wwjCommon/exception/business.exception';
// @UploadedFile() - 单文件上传，配合 @UseInterceptors(FileInterceptor('file'))
// @UploadedFiles() - 多文件上传，配合 @UseInterceptors(FilesInterceptor('files'))
// @Session() - 获取会话对象，对应PHP Session::get()
// @Req() - 获取Request对象，对应PHP Request
import { NiucloudService } from '../../services/admin/niucloud.service';
import { CoreAuthService } from '../../services/core/core-auth.service';

/**
 * ModuleController
 * 对应 PHP: Module Controller
 * 对应 Java: @RestController
 *
 * 支持装饰器：
 * - @UploadedFile() - 单文件上传 (对应PHP Request::file())
 * - @UploadedFiles() - 多文件上传
 * - @Session() - 会话管理 (对应PHP Session::get())
 * - @Req() - 请求对象 (对应PHP Request)
 */
@ApiTags('niucloud')
@Controller('adminapi/niucloud')
export class ModuleController {
  constructor(
    private readonly niucloudService: NiucloudService,
    private readonly coreAuthService: CoreAuthService,
  ) {}

  /**
   * 模块
   * 路由: GET authinfo
   * PHP路由: Route::get('authinfo', 'niucloud.Module/authorize')
   */
  @Get('authinfo')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '模块' })
  async authorize(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: authorize

      return await this.coreAuthService.getAuthInfo(id);
    } catch (error) {
      throw new BusinessException('authorize操作失败', error);
    }
  }

  /**
   * 模块
   * 路由: POST authinfo
   * PHP路由: Route::post('authinfo', 'niucloud.Module/setAuthorize')
   */
  @Post('authinfo')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '模块' })
  async setAuthorize(@Body() data: SetAuthorizeDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: setAuthorize

      return await this.niucloudService.setAuthorize(data);
    } catch (error) {
      throw new BusinessException('setAuthorize操作失败', error);
    }
  }

  /**
   * 模块
   * 路由: GET admin/authinfo
   * PHP路由: Route::get('admin/authinfo', 'niucloud.Module/getAuthorize')
   */
  @Get('admin/authinfo')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '模块' })
  async getAuthorize(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getAuthorize

      return await this.niucloudService.getAuthorize();
    } catch (error) {
      throw new BusinessException('getAuthorize操作失败', error);
    }
  }

  /**
   * 模块
   * 路由: GET framework/newversion
   * PHP路由: Route::get('framework/newversion', 'niucloud.Module/getFrameworkLastVersion')
   */
  @Get('framework/newversion')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '模块' })
  async getFrameworkLastVersion(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getFrameworkLastVersion

      return await this.niucloudService.getFrameworkLastVersion();
    } catch (error) {
      throw new BusinessException('getFrameworkLastVersion操作失败', error);
    }
  }

  /**
   * 模块
   * 路由: GET framework/version/list
   * PHP路由: Route::get('framework/version/list', 'niucloud.Module/getFrameworkVersionList')
   */
  @Get('framework/version/list')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '模块' })
  async getFrameworkVersionList(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getFrameworkVersionList

      return await this.niucloudService.getFrameworkVersionList();
    } catch (error) {
      throw new BusinessException('getFrameworkVersionList操作失败', error);
    }
  }

  /**
   * 模块
   * 路由: GET app_version/list
   * PHP路由: Route::get('app_version/list', 'niucloud.Module/getAppVersionList')
   */
  @Get('app_version/list')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '模块' })
  async getAppVersionList(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getAppVersionList

      return await this.niucloudService.getAppVersionList(data['app_key']);
    } catch (error) {
      throw new BusinessException('getAppVersionList操作失败', error);
    }
  }
}
